package beautiful.butterfly.drds.data_exchange.data_exchange.record.column;

import beautiful.butterfly.drds.data_exchange.data_exchange.record.column.column_cast.ColumnCast;
import beautiful.butterfly.drds.data_exchange.error_code.CommonErrorCode;
import beautiful.butterfly.drds.data_exchange.error_code.DataExchangeException;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;


public class DateColumn extends Column
{

    @Setter
    @Getter
    private DateType dateType = DateType.DATETIME;
    //

    /**
     * 构建值为null的DateColumn，使用Date子类型为DATETIME
     */
    public DateColumn()
    {
        this((Long) null);
    }

    /**
     * 构建值为stamp(Unix时间戳)的DateColumn，使用Date子类型为DATETIME
     * 实际存储有date改为long的ms，节省存储
     */
    public DateColumn(final Long aLong)
    {
        //底层类型是long
        super(aLong, ColumnType.DATE, (null == aLong ? 0 : 8));
    }

    /**
     * 构建值为date(java.util.Date)的DateColumn，使用Date子类型为DATETIME
     */
    public DateColumn(final Date date)
    {
        this(date == null ? null : date.getTime());
    }

    /**
     * 构建值为date(java.sql.Date)的DateColumn，使用Date子类型为DATE，只有日期，没有时间
     */
    public DateColumn(final java.sql.Date date)
    {
        this(date == null ? null : date.getTime());
        this.setDateType(DateType.DATE);
    }

    /**
     * 构建值为time(java.sql.Time)的DateColumn，使用Date子类型为TIME，只有时间，没有日期
     */
    public DateColumn(final java.sql.Time time)
    {
        this(time == null ? null : time.getTime());
        this.setDateType(DateType.TIME);
    }

    /**
     * 构建值为ts(java.sql.Timestamp)的DateColumn，使用Date子类型为DATETIME
     */
    public DateColumn(final java.sql.Timestamp timestamp)
    {
        this(timestamp == null ? null : timestamp.getTime());
        this.setDateType(DateType.DATETIME);
    }

    @Override
    public Boolean asBoolean()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Date类型不能转为Boolean .");
    }

    @Override
    public Long asLong()
    {

        return (Long) this.getObject();
    }

    @Override
    public Double asDouble()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Date类型不能转为Double .");
    }

    @Override
    public BigDecimal asBigDecimal()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Date类型不能转为BigDecimal .");
    }


    @Override
    public BigInteger asBigInteger()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Date类型不能转为BigInteger .");
    }

    @Override
    public String asString()
    {
        try
        {
            return ColumnCast.date2String(this);
        } catch (Exception e)
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT,
                    String.format("Date[%s]类型不能转为String .", this.toString()));
        }
    }

    @Override
    public Date asDate()
    {
        if (null == this.getObject())
        {
            return null;
        }

        return new Date((Long) this.getObject());
    }

    @Override
    public byte[] asBytes()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Date类型不能转为Bytes .");
    }

}